<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTableList">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tables" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column type="index" width="60"></el-table-column>
			<el-table-column prop="name" label="表名" width="120" sortable></el-table-column>
			<el-table-column prop="description" label="描述" width="160" sortable></el-table-column>
			<el-table-column prop="tableType" label="表格类型" width="130" sortable></el-table-column>
			<el-table-column prop="formType" label="表单类型" width="130" sortable></el-table-column>
			<!-- <el-table-column prop="pcStyle" label="PC表单风格" width="140" sortable></el-table-column>
			<el-table-column prop="mobileStyle" label="手机表单风格" width="140" sortable></el-table-column>
			<el-table-column prop="isPage" label="是否分页" width="100" ></el-table-column> -->
			<el-table-column prop="createUserName" label="创建人" min-width="100" ></el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope"> 
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">生成后台代码</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" top="8vh" width="80%" :visible.sync="editFormVisible" :close-on-click-modal="false" >
			
			<sysTableDetail :formData="editForm" ref="editForm" :validatoRule="formRules" :addColumn="addColumn" :deleteColumn="deleteColumn">
			</sysTableDetail>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog> 
		

		<!--新增界面-->
		<el-dialog title="新增" top="8vh" width="80%" :visible.sync="addFormVisible" :close-on-click-modal="false" >
			
			<sysTableDetail ref="addForm" :formData="addForm" :validatoRule="formRules" :addColumn="addColumn" :deleteColumn="deleteColumn">
			</sysTableDetail>
			<div slot="footer" class="dialog-footer">

				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		
		
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import sysTableDetail from '../../components/sysTableDetail.vue'
	import { getSysTableList, addSysTable, deleteSysTable, updateSysTable, deleteMuti } from '../../api/api';
	var tableDefaultColumnList = require('../../json/tableDefaultColumn.json');
	var dict = require('../../json/dict.json')


	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				tables: [],
				total: 0,
				page: 1,
				pageSize: 10,
				listLoading: false,
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: { name: '' },
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				formRules: {
					name: [
						{ required: true, message: '请输入', trigger: 'blur' },
			            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
					],
					description: [
						{ required: true, message: '请输入', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: { 
					name: '', 
					tableType: '单表',
					formType: '普通表单',
					isPage: '是'
				}

			}
		},
		components: {
            'sysTableDetail': sysTableDetail
        },
		methods: {
			handleCurrentChange(val) {
				this.page = val;
				this.getTableList();
			},
			
			getTableList() {
				let para = {
					pageNo: this.page,
					pageSize: this.pageSize,
					name: this.filters.name
				};
				getSysTableList(para, this.$router).then((res) => {
					this.total = res.totalCount;
					this.tables = res.list;
					this.listLoading = false;
				});

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					deleteSysTable(para, this.$router).then((res) => {
						this.listLoading = false;
						if(res.success){
							
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getTableList();
						}else{
							this.$alert(res.msg, '操作失败', {});
						}
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm.sysTableColumnList = tableDefaultColumnList;
				
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.getForm().validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;

							this.editForm.createTime = (!this.editForm.createTime || this.editForm.createTime == '') ? '' : util.formatDate.format(new Date(this.editForm.createTime), 'yyyy-MM-dd');
							this.editForm.updateTime = (!this.editForm.updateTime || this.editForm.updateTime == '') ? '' : util.formatDate.format(new Date(this.editForm.updateTime), 'yyyy-MM-dd');

							updateSysTable(this.editForm, this.$router).then((res) => {
								this.editLoading = false;
								if(res.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.editFormVisible = false;
									this.getTableList();
								}else{
									this.$alert(res.msg, '操作失败', {});
								}
								
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.getForm().validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
							addSysTable(para, this.$router).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								if(res.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.addFormVisible = false;
									this.getTableList();
								}else{
									this.$alert(res.msg, '操作失败', {});
								}
								
								// this.$refs['addForm'].resetFields();
								
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true; 
					//NProgress.start();
					let para = { ids: ids };
					deleteMuti(para, this.$router).then((res) => {
						this.listLoading = false;
						if(res.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getTableList();
						}else{
							this.$alert(res.msg, '操作失败', {
					          	confirmButtonText: '确定',
					          	callback: action => {
					            	this.$message({
					              		type: 'info',
					              		message: `action: ${ action }`
					            	});
					          	}
					        });
						}
					});
				}).catch(() => {

				}); 
			},
			addColumn: function(){	
				if(this.addFormVisible){
					this.addForm.sysTableColumnList.push({
						'columnIndex': this.addForm.sysTableColumnList.length + 1,
						'columnType': 'varchar',
						'columnLength': '50',
						'columnDecimal': '0',
						'isPrimeryKey': 'false',
						'isAllowNull': 'true'
					});
				}else{
					this.editForm.sysTableColumnList.push({'columnIndex': this.editForm.sysTableColumnList.length + 1});
				}
			},
			deleteColumn: function(){
				var detailSels = [];
				if(this.addFormVisible){
					detailSels = this.$refs.addForm.sels;
					for(var i=0; i<detailSels.length; i++){
						if(detailSels[i].columnIndex > 10){
							this.addForm.sysTableColumnList.splice((detailSels[i].columnIndex-1), 1);
							if(this.addForm.sysTableColumnList.length >= detailSels[i].columnIndex){
								for(var j=(detailSels[i].columnIndex-1); j<this.addForm.sysTableColumnList.length; j++){
									this.addForm.sysTableColumnList[j].columnIndex = (j+1);
								}
							}
						}
					}
				}else{
					detailSels = this.$refs.editForm.sels;
					for(var i=0; i<detailSels.length; i++){
						if(detailSels[i].columnIndex > 10){
							this.editForm.sysTableColumnList.splice((detailSels[i].columnIndex-1), 1);
							if(this.editForm.sysTableColumnList.length >= detailSels[i].columnIndex){
								for(var j=(detailSels[i].columnIndex-1); j<this.editForm.sysTableColumnList.length; j++){
									this.editForm.sysTableColumnList[j].columnIndex = (j+1);
								}
							}
						}
					}
				}
				
			}
		},
		mounted() {
			
			this.getTableList();
		}
	}

</script>
<style>
	.mydialog {
	    width: 80%;
	}
</style>
